<?php

class IndexAdmin extends BaseAction {

    public function actionSortList() {
        $page = empty($this->params['page']) ? 1 : $this->params['page'];
        $pagelimit = 10;
        $offset = ($page - 1) * $pagelimit;
       
        $this->setPageTitle('下载分类');
        $articles = DAO_Finder::find(DB_DOWN_CATE, null, '*', 'id DESC', "$offset, $pagelimit");
        
        $count = DAO_Finder::findCount(DB_DOWN_CATE);
        $pager = new Pager(url_for('admin/m/index/sort_list/page/'), $count, $pagelimit);
        $pager->SetCurPage($page);
        $this->set('articles', $articles);
        $this->set('pager', $pager);
        $this->render(null, true);
    }
    public function actionList() {
        $page = empty($this->params['page']) ? 1 : $this->params['page'];
        $pagelimit = 10;
        $offset = ($page - 1) * $pagelimit;

        $this->setPageTitle('下载管理');
        $articles = DAO_Finder::find(DB_DOWN_LOAD, null, '*', 'id DESC', "$offset, $pagelimit");

        $count = DAO_Finder::findCount(DB_DOWN_LOAD);
        $pager = new Pager(url_for('admin/m/index/list/page/'), $count, $pagelimit);
        $pager->SetCurPage($page);
        $this->set('articles', $articles);
        $this->set('pager', $pager);
        $this->render(null, true);
    }
    
    public function actionEdit() {
        $id = empty($this->params['id']) ? $_POST['id'] : $this->params['id'];
        if (!empty($id)) {
            $article = ORM_Finder::findById('DB_DOWN_LOAD', $id);
            $this->setPageTitle('下载修改');
        } else {
            $article = new DB_DOWN_LOAD();
            $this->setPageTitle('下载新增');
        }
        $cate   = ORM_Finder::find(DB_DOWN_CATE, null, 'id,title');

        if (!empty($_POST)) {
           if (!empty($_FILES['pic']['name'])) {
                $pic = $_FILES['pic'];
                $ext = strtolower(substr($pic['name'], strrpos($pic['name'], '.') + 1));
                if ($ext != "jpg" && $ext != 'gif' && $ext != 'png')
                {
                        //die("illegal file");
                }
                
                $newfilename = time().'.'.$ext;
                $dir    =  '/' .date('Ym/d/');
                echo ROOT . '/static'. $dir.$newfilename.'<br/>';
                if(!is_dir(ROOT .'/static/'. $dir)) {
                    mkdir(ROOT .'/static/'. $dir, 0777, true);
                }
                move_uploaded_file($pic['tmp_name'], ROOT . '/static'. $dir.$newfilename);
                $article->setLocalUrl($dir.$newfilename);
            }
            $title      = trim($_POST['title']);
            $content    = trim($_POST['content']);
            $description = trim($_POST['description']);
            $tags       =   trim($_POST['tags']);
            $date       = strtotime($_POST['add_time']);
            $dbank_url  = $_POST['dbank_url'];
            $u115_url   = $_POST['u115_url'];
            $status     = $_POST['status'];
            $key_word   = $_POST['key_word'];
            $down_name  = $_POST['down_name'];
            $local_url  = $_POST['local_url'];
            $status     = $_POST['state'];
            
            $article->setTitle($title);
            $article->setContent($content);
            $article->setDescription($description);
            $article->setTags($tags);
            $article->setStatus($status);
            $article->setKeyWord($key_word);
            $article->setDownName($down_name);
            $article->setU115Url($u115_url);
            $article->setDbankUrl($dbank_url);
            $article->setStatus($status);

            if(empty($_FILES['pic'])) {
                $article->setLocalUrl($local_url);
            }
            
            if ($article->isNew() && empty($date)) {
                $date = time();
            }
            $article->setAddTime($date);
            $article->save();
            if(empty($down_name)) {
                $article->setDownName($article->getId());
            }
            $this->redirect('admin/m/index/list/');
        }
        
        $this->set('article', $article);
        $this->set('cate', $cate);
        $this->render(null,true);
    }
    
    public function actionSortDel() {
        $id = $this->params['id'];
        if (!empty($id)) {
            $article = ORM_Finder::findById(DB_DOWN_CATE, $id);
            if (!empty($article)) {
                //ArticleTag::deleteWithArticle($article->getId());
                $article->delete();
            }
        }
        $this->redirect($_SERVER['HTTP_REFERER']);
    }
    public function actionDel() {
        $id = $this->params['id'];
        if (!empty($id)) {
            $article = ORM_Finder::findById('Article', $id);
            if (!empty($article)) {
                ArticleTag::deleteWithArticle($article->getId());
                $article->delete();
            }
        }
        $this->redirect($_SERVER['HTTP_REFERER']);
    }
}
?>